﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Serialization;
using System.IO;

namespace MyMovieInfo
{
    class MovieResolver
    {
        private List<Movie> _movies = new List<Movie>();
        private readonly IList<IMovieLookup> _lookupServices = new List<IMovieLookup>();
        
        public MovieResolver()
        { }

        public void RegisterLookupService(IMovieLookup service)
        {
            _lookupServices.Add(service);
        }        

        public List<Movie> Movies
        {
            get { return this._movies; }
        }

        
        public void QueryMovieServices(List<string> movieFileNames)
        {
            IMovieListCreator queryCreator = new MovieListFromFileNames(movieFileNames);
            List<Movie> queries = queryCreator.CreateMovieQueries();

            foreach (var query in queries)
            {
                var movie = new Movie();
                foreach (IMovieLookup service in _lookupServices)
                {
                    movie = service.GetMovieDetails(query.Name, query.Year, query.FilePath);
                    if (movie.MatchFound)
                        break;
                }
                _movies.Add(movie);
            }
        }

        //SRP - violation
        public void SaveMovieData(string theFileName)
        { 
            XmlSerializer xmlserializer = new XmlSerializer(typeof(List<Movie>));
            using (StreamWriter streamwriter = new StreamWriter(theFileName))
            {
                xmlserializer.Serialize(streamwriter, _movies);
                streamwriter.Close();
            }
        }
    }
}